package com.mall.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.commons.exceotion.BusinessException;
import com.mall.commons.exceotion.ErrorResult;
import com.mall.dubbo.mappers.OmsCompanyAddressMapper;
import com.mall.dubbo.mappers.ReturnApplyMapper;
import com.tanhua.model.domain.OmsCompanyAddress;
import com.tanhua.model.domain.OmsOrderReturnApply;
import com.tanhua.model.dto.OmsOrderReturnApplyDto;
import com.tanhua.model.dto.OmsOrderReturnApplyUpdateDto;
import com.tanhua.model.dto.ReturnApplyDto;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@DubboService
public class ReturnApplyApiImpl implements ReturnApplyApi {

    @Autowired
    private ReturnApplyMapper returnApplyMapper;

    @Autowired
    private OmsCompanyAddressMapper companyAddressMapper;

    /**
     * 分页查询退货申请
     *
     * @param returnApplyDto
     * @return
     */
    @Override
    public Page listOmsOrderReturnApply(ReturnApplyDto returnApplyDto) {
        Page<OmsOrderReturnApply> page = new Page(returnApplyDto.getPageNum(), returnApplyDto.getPageSize());
        LambdaQueryWrapper<OmsOrderReturnApply> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(StringUtils.isNotEmpty(returnApplyDto.getCreateTime()), OmsOrderReturnApply::getCreateTime, returnApplyDto.getCreateTime())
                .in(StringUtils.isNotEmpty(returnApplyDto.getHandleMan()), OmsOrderReturnApply::getHandleMan, returnApplyDto.getHandleMan())
                .in(StringUtils.isNotEmpty(returnApplyDto.getHandleTime()), OmsOrderReturnApply::getHandleTime, returnApplyDto.getHandleTime())
                .in(null != returnApplyDto.getId(), OmsOrderReturnApply::getId, returnApplyDto.getId())
                .in(null != returnApplyDto.getStatus(), OmsOrderReturnApply::getStatus, returnApplyDto.getStatus());
        return returnApplyMapper.selectPage(page, queryWrapper);
    }
    /**
     * 获取退货申请详情
     *
     * @param id
     * @return
     */
    @Override
    public OmsOrderReturnApplyDto getById(Integer id) {
        if (id == null) {
            throw new BusinessException(ErrorResult.emptyError());
        }
        OmsOrderReturnApply select = returnApplyMapper.selectById(id);
        //创建dto对象
        OmsOrderReturnApplyDto dto = new OmsOrderReturnApplyDto();
        //拷贝对象
        BeanUtils.copyProperties(select, dto);
        LambdaQueryWrapper<OmsCompanyAddress> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(OmsCompanyAddress::getId, select.getCompanyAddressId());
        List<OmsCompanyAddress> list = companyAddressMapper.selectList(wrapper);
        dto.setCompanyAddress(list);

        return dto;
    }

    /**
     * 修改退货申请状态
     *
     * @param id
     * @param dto
     * @return
     */
    @Override
    public boolean updates(Integer id, OmsOrderReturnApplyUpdateDto dto) {
        if (id == null) {
            return false;
        }
        OmsOrderReturnApply byId = returnApplyMapper.selectById(id);
        BeanUtils.copyProperties(dto, byId);
        byId.setReceiveTime(new Date(System.currentTimeMillis()));
        returnApplyMapper.updateById(byId);
        return true;
    }

    /**
     * 批量删除退货申请
     *
     * @param ids
     * @return
     */
    @Override
    public boolean deletes(Integer[] ids) {
        if (ids == null) {
            return false;
        }
        //订单状态为 已完成的才可以删除 status=2
        for (Integer id : ids) {
            if (returnApplyMapper.selectById(id).getStatus() != 2) {
                throw new BusinessException(ErrorResult.nullError());
            }
        }
        returnApplyMapper.deleteBatchIds(Arrays.asList(ids));
        return true;
    }
}
